package com.mmall.controller.portal;
/**
 * Created by 果果 on 2017/8/2.
 */

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author 果果
 * @描述 用户模块controller
 * @create 2017-08-02 21:55
 **/
@Controller
@RequestMapping("/user/")
public class UserController {
    @Autowired
    private IUserService userService;
    /**
     * 用户登录
     * @param username
     * @param password
     * @param session
     * @return
     */
    @RequestMapping(value ="login.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> login(String username, String password, HttpSession session){
        ServerResponse<User> response=userService.login(username,password);
        if (response.isSuccess()){
           session.setAttribute(Const.CURRENT_USER,response.getData());
        }
         return  response;
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @RequestMapping(value="register.do",method=RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> register(User user){
        return userService.register(user);
    }

    /**
     * 验证用户名和邮箱是否重复
     * @param str
     * @param type
     * @return
     */
    @RequestMapping(value="check_valid.do",method = RequestMethod.POST)
    @ResponseBody
    public  ServerResponse<String> check_vaild(String str,String type){
       return  userService.checkValid(str,type);
    }
    /**
     * 退出操作
     * @param session
     * @return
     */
    @RequestMapping(value = "logout.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> logout(HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        return ServerResponse.createBySuccess();
    }

    /**
     * 获取当前登录用户的信息  ---首首页展示登录的用户，名使用
     * @param session
     * @return
     */
    @RequestMapping(value = "get_user_info.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> getUserInfo(HttpSession session){
      User user=(User)session.getAttribute(Const.CURRENT_USER);
      if(user!=null){
          return ServerResponse.createBySuccess(user);
      }
      return  ServerResponse.createByErrorMessage("用户未登录，不能获取到当前用户的信息");
    }

    /**
     * 根据用户名查询用户的忘记密码的问题
     * @param username
     * @return
     */
    @RequestMapping(value="forget_get_question.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> forgetGetQuestion(String username){

        return userService.SelectQuestion(username);
    }

    /**
     * 检查用户的答案是否正确
     * @param username
     * @param question
     * @param answer
     * @return
     */
    @RequestMapping(value="forget_check_answer.do",method=RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> forgetCheckAnswer(String username,String question,String answer){
        return userService.forgetCheckAnswer(username,question,answer);
    }

    /**
     * 忘记密码时的修改密码的提交
     * @param username
     * @param passwordNew
     * @param forgetToken
     * @return
     */
    @RequestMapping(value="forget_reset_password.do",method=RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> forgetResetPassword(String username,String passwordNew,String forgetToken){
        return userService.forgetResetPassword(username,passwordNew,forgetToken);
    }

    /**
     * 修改密码的提交
     * @param session
     * @param passwordOld
     * @param passwordNew
     * @return
     */
    @RequestMapping(value="reset_password.do",method=RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> resetPassword(HttpSession session,String passwordOld,String passwordNew){
        User user=(User)session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return  ServerResponse.createByErrorMessage("用户未登录");
        }
        return userService.resetPassword(passwordOld,passwordNew,user);
    }

    /**
     * 更新用戶的基本信息
     * @param session
     * @param user
     * @return
     */
    @RequestMapping(value="updateUserInfo.do",method=RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> updateUserInfo(HttpSession session,User user){
        User currentUser=(User)session.getAttribute(Const.CURRENT_USER);
        if (currentUser==null){
            return ServerResponse.createByErrorMessage("用户未登录");
        }
        //用户名是不能被修改的
        user.setId(currentUser.getId());
        user.setUsername(currentUser.getUsername());
        //调用
        ServerResponse<User> response=userService.updateUserInfo(user);
        if (response.isSuccess()){
            response.getData().setUsername(currentUser.getUsername());
            session.setAttribute(Const.CURRENT_USER,response.getData());
        }
       return  response;
    }

    /**
     * 获取用户的信息
     * @param session
     * @return
     */
    @RequestMapping(value="get_Information.do",method=RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> get_Information(HttpSession session){
        User currentUser=(User)session.getAttribute(Const.CURRENT_USER);
        if (currentUser==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"未登录，需要强制登录");
        }
        return userService.get_Information(currentUser.getId());

    }
}
